<?php
/*
说明 : 这里是数据库操作的公共类
*/

class MySqlDb
{
    private $link;
    private static $instance = null;

   public function __construct($host, $port, $user, $pws, $charset, $dbname)
    {
        /**
         * 初始化数据库
         */
        $this->link = mysqli_connect("$host:$port", "$user", "$pws",$dbname);
        mysqli_set_charset($this->link,$charset);

        // $this->link = mysqli_connect("$host:$port", "$user", "$pws",$dbname);
        // $link = mysqli_connect("127.0.0.1","root","123456","shunyi");
        // mysql_query("set names $charset");
        // mysql_select_db("$dbname");
    }
    /**
     * 所有的数据库方法
     */
    private function query($sql)
    {
        $res = mysqli_query($this->link,$sql);
        if ($res === false) {
            echo "<p>sql语句执行失败：<br>错误信息：" . mysqli_error($this->link);
            die();
        }
        return $res;
    }

    public function getOne($sql)
    {
        $res = $this->query($sql);
        $re = mysqli_fetch_assoc($res);
       
        return $re;
    }

    public function getAll($sql)
    {

        $res = $this->query($sql);
        $arr = array();
        while ($re = mysqli_fetch_assoc($res)) {
            $arr[] = $re;
        }
        return $arr;
    }

    public function exec($sql)
    {   
        $res = $this->query($sql);
        return $res;
    }


}


?>